package org.jsola.hr.helper;

import org.jsola.common.ClassKit;
import org.jsola.common.DateKit;
import org.jsola.common.SafeKit;
import org.jsola.hr.dto.AttendanceRecordAddDTO;
import org.jsola.hr.dto.AttendanceRecordUpdateDTO;
import org.jsola.hr.entity.AttendanceRecordDO;
import org.jsola.hr.query.AttendanceRecordQuery;
import org.jsola.hr.service.IAttendanceRecordService;
import org.jsola.hr.vo.AttendanceRecordVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.List;

import static org.jsola.hr.core.AbstractTest.SITE_ID;



/**
 * @author zhr
 */
@Component
public class AttendanceRecordTestHelper extends BaseTestHelper {
    
    @Autowired
    private IAttendanceRecordService attendanceRecordService;

    /**
     * 生成add测试数据
     * @return AttendanceRecordAddDTO
     */
    public AttendanceRecordAddDTO getAttendanceRecordAddDTO(){
        AttendanceRecordAddDTO attendanceRecordAddDTO = new AttendanceRecordAddDTO();
        attendanceRecordAddDTO.setEmpId(getRandomValue("varchar",32));
        attendanceRecordAddDTO.setNumber(getRandomValue("varchar",32));
        attendanceRecordAddDTO.setDate((Date)ClassKit.getObjectValue(getRandomValue("date",0),"java.util.Date"));
        attendanceRecordAddDTO.setStartTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        attendanceRecordAddDTO.setEndTime((Date)ClassKit.getObjectValue(getRandomValue("datetime",0),"java.util.Date"));
        attendanceRecordAddDTO.setStatus(SafeKit.getInteger(getRandomValue("tinyint",4)));
        attendanceRecordAddDTO.setRemark(getRandomValue("varchar",255));
        attendanceRecordAddDTO.setCompanyId(getRandomValue("varchar",32));
        return attendanceRecordAddDTO;
    }


    /**
     * 生成update测试数据
     * @return AttendanceRecordUpdateDTO
     */
    public AttendanceRecordUpdateDTO getAttendanceRecordUpdateDTO(AttendanceRecordVO attendanceRecordVO){
        return attendanceRecordService.selectDOById(attendanceRecordVO.getId(), SITE_ID).to(AttendanceRecordUpdateDTO.class);
    }

    /**
     * 生成查询数据
     * @return AttendanceRecordQuery
     */
    public AttendanceRecordQuery getAttendanceRecordQuery() {
        AttendanceRecordQuery attendanceRecordQuery = new AttendanceRecordQuery();
        List<AttendanceRecordDO> attendanceRecordDOList = attendanceRecordService.list(new AttendanceRecordDO(),SITE_ID);
        if (!CollectionUtils.isEmpty(attendanceRecordDOList)) {
            AttendanceRecordDO attendanceRecordDO = attendanceRecordDOList.get(0);
            attendanceRecordQuery = attendanceRecordDO.to(AttendanceRecordQuery.class);
            if (attendanceRecordDO.getDate() != null) {
                attendanceRecordQuery.setDateStart(DateKit.getBeforeDay(attendanceRecordDO.getDate(),-1));
                attendanceRecordQuery.setDateEnd(DateKit.getBeforeDay(attendanceRecordDO.getDate(),1));
            }
            if (attendanceRecordDO.getGmtCreate() != null) {
                attendanceRecordQuery.setGmtCreateStart(DateKit.getBeforeDay(attendanceRecordDO.getGmtCreate(),-1));
                attendanceRecordQuery.setGmtCreateEnd(DateKit.getBeforeDay(attendanceRecordDO.getGmtCreate(),1));
            }
            if (attendanceRecordDO.getGmtModified() != null) {
                attendanceRecordQuery.setGmtModifiedStart(DateKit.getBeforeDay(attendanceRecordDO.getGmtModified(),-1));
                attendanceRecordQuery.setGmtModifiedEnd(DateKit.getBeforeDay(attendanceRecordDO.getGmtModified(),1));
            }
        }
        return attendanceRecordQuery;
    }

    /**
     * 保存示例
     * @return AttendanceRecordVO
     */
    public AttendanceRecordVO saveAttendanceRecordExample(){
        AttendanceRecordAddDTO addDTO = this.getAttendanceRecordAddDTO();
        AttendanceRecordVO attendanceRecordVO = attendanceRecordService.save(addDTO,buildTokenUser());
        return attendanceRecordVO;
    }


}

